goog.provide('BombBall.Ball');

goog.require('Stefan.Circle');
goog.require('Stefan.fill.Border');

BombBall.Ball = T.lang.createClass(function(config){
	Stefan.Circle.call(this);
	this.init(config);
}, {
	superClass: Stefan.Circle
}).extend({
	init: function (config) {
		var that = this;
		this.config = config;
		this.map = config.map;
		this.x = config.x || 200;
		this.y = config.y || 200;
		this.r = 25;
		this.draggable = config.draggable;
		this.speed_x = config.speed_x || parseInt(Math.random() * 500);
		this.speed_y = config.speed_y || parseInt(Math.random() * 500);
		this.setSize(this.r*2,this.r*2);
		this.setAnchor(.5, .5);
		this.setBorder(new Stefan.fill.Border({
			width: 2
			,color: '#333'
		}));
		var color = config.color || '#000000';
		this.setFill(color);
		this.setPosition(this.x, this.y);
		this.map.balls.push(this);
		this.map.appendChild(this);
		
		/**
		 * addListener
		 */
		this.isActive = false;
		this.activeX = this.x;
		this.activeY = this.y;
		this.on("mousedown",function(){
			if(!that.draggable){
				return;
			}
			that.isActive = true;
			that.setFill('rgb(0,0,255,.8)');
		});
		this.on("mousemove",function(e){
			if(that.isActive){
				
			}
		});
		T.on(document,"mouseup",function(e){
			if(!that.draggable){
				return;
			}
			if(that.isActive){
				that.setFill(that.config.color);
				that.isActive = false;
				that.speed_x = (that.getPosition().x - (e.offsetX - that.map.getPosition().x)) * 5;
				that.speed_y = (that.getPosition().y - (e.offsetY - that.map.getPosition().y)) * 5;
				//console.info(that.speed_x + "  "+that.speed_y);
			}
		});
	},
	
	toNextPosition: function() {
		if(this.x < 80 && this.y <80){
			this.map.removeChild(this);
			T.array.remove(this.map.balls, this);
			if(this.map.time < 0){
				return;
			}
			T.g("score").innerHTML = parseInt(T.g("score").innerHTML)+1;
			return;
		}
		this.x += this.speed_x/100;
		this.y += this.speed_y/100;
		if(this.x-this.r < 0 || this.x+this.r>BombBall.STAGE_WIDTH) {
			this.speed_x = -this.speed_x;
		}
		if(this.y-this.r < 0 || this.y+this.r>BombBall.STAGE_HEIGHT) {
			this.speed_y = -this.speed_y;
		}
		if(this.speed_x != 0) {
			if(this.speed_x < 0) {
				this.speed_x += 2;
			} else {
				this.speed_x -= 2;
			}
		}
	
		if(this.speed_y != 0) {
			if(this.speed_y < 0) {
				this.speed_y += 2;
			} else {
				this.speed_y -= 2;
			}
		}
		this.setPosition(this.x, this.y);
	},
	step: function(){
		this.toNextPosition();
	}
});

BombBall.Ball.judgeCrash = function(a,b){
	var x = a.x - b.x;
	var y = a.y - b.y;
	if(Math.sqrt(x*x + y*y) <= a.r+b.r){
		/**
		 * 按照公式计算碰撞后的速度
		 */
		var v1x = a.speed_x;
		var v1y = a.speed_y;
		var v2x = b.speed_x;
		var v2y = b.speed_y;
		var x1 = a.x;
		var x2 = b.x;
		var y1 = a.y;
		var y2 = b.y;
		
		a.speed_x = (v1x*(y2-y1)*(y2-y1) + v2x*(x2-x1)*(x2-x1)+(v2y-v1y)*(x2-x1)*(y2-y1))/((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));

		a.speed_y = (v1y*(x2-x1)*(x2-x1) + v2y*(y2-y1)*(y2-y1)+(v2x-v1x)*(x2-x1)*(y2-y1))/((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));

		b.speed_x = (v1x*(x2-x1)*(x2-x1) + v2x*(y2-y1)*(y2-y1)-(v2y-v1y)*(x2-x1)*(y2-y1))/((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));

		b.speed_y = (v1y*(y2-y1)*(y2-y1) + v2y*(x2-x1)*(x2-x1)-(v2x-v1x)*(x2-x1)*(y2-y1))/((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
	}
};
